import gtk
import gtk.gdk

def rounding(dig):
    if dig-int(dig) >= 0.5:
        return int(dig)+1
    else:
        return int(dig)

def pix_to_mm(pix, type = 0):
    """if type = 0 ,is horizontal
        if type = 1 ,is vertical"""
    if type == 0:
        return pix*(gtk.gdk.screen_width_mm()/float(gtk.gdk.screen_width()))
    else:
        return pix*(gtk.gdk.screen_height_mm()/float(gtk.gdk.screen_height()))

def mm_to_pix(mm, type = 0):
    """if type = 0 ,is horizontal
        if type = 1 ,is vertical"""
    if type == 0:
        return mm*gtk.gdk.screen_width()/float(gtk.gdk.screen_width_mm())
    else:
        return mm*gtk.gdk.screen_height()/float(gtk.gdk.screen_height_mm())

def pix_to_in(pix, type = 0):
    """if type = 0 ,is horizontal
        if type = 1 ,is vertical"""
    return pix_to_mm(pix, type)/10/2.539999918

def in_to_pix(inch, type = 0):
    """if type = 0 ,is horizontal
        if type = 1 ,is vertical"""
    return mm_to_pix(inch*2.539999918*10, type)

# #########################################################################################
# fun(draw, gc, start, length, grad_start, grad_end, space, type)
# draw:
# gc:
# start:
# length:
# grad_start:
# grad_end:
# space:
# type:
# #########################################################################################

def draw_graduation_cm(draw, gc, start, length, grad_start, grad_end, space, type = 0):
    for i in xrange(0, int(pix_to_mm(length))+1, space):
            if type == 0:
                draw.window.draw_line(gc, start+rounding(mm_to_pix(i, type)), rounding(grad_start),
                                        start+rounding(mm_to_pix(i, type)), rounding(grad_end))
            else:
                draw.window.draw_line(gc, rounding(grad_start), start+rounding(mm_to_pix(i, type)),
                                        rounding(grad_end), start+rounding(mm_to_pix(i, type)))

def draw_graduation_px(draw, gc, start, length, grad_start, grad_end, space, type = 0):
    for i in xrange(0, length, space):
        if type == 0:
            draw.window.draw_line(gc, start+i, grad_start,
                                            start+i, grad_end)
        else:
            draw.window.draw_line(gc, grad_start, start+i,
                                            grad_end, start+i)

def draw_graduation_in(draw, gc, start, length, grad_start, grad_end, space, type = 0):
    count = 0
    while space < 10:
        if space >= 1:
            for i in xrange(0, int((pix_to_in(length))*(10**count)*(10**count)), int(space*(10**count))):
                if type == 0:
                    draw.window.draw_line(gc, start+int(in_to_pix(i)/(10**count)/(10**count)), grad_start,
                                                start+int(in_to_pix(i)/(10**count)/(10**count)), grad_end)
                else:
                    draw.window.draw_line(gc, grad_start, start+int(in_to_pix(i)/(10**count)/(10**count)),
                                                grad_end, start+int(in_to_pix(i)/(10**count)/(10**count)))
        space *= 10
        count += 1
